package com.aaa.Service.Impl;

import com.aaa.Dao.UserDao;
import com.aaa.Entity.User;
import com.aaa.Service.UserService;
import com.aaa.util.DefaultMsg;
import com.aaa.util.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;


    public User findUAndP(User user) {
        return userDao.findUAndP(user);
    }

    public TableData<User> page(String keyword, Integer page, Integer limit) {
        TableData<User> tableData=new TableData<User>();
        Integer offset=(page-1)*limit;
        List<User> data = userDao.listPage(keyword, offset, limit);
        tableData.setData(data);
        Integer count=userDao.count(keyword);
        tableData.setCount(count);
        return tableData;
    }

    public DefaultMsg saveOrUpdate(User user) {
        Integer count=0;
        DefaultMsg defaultMsg = new DefaultMsg();
        if(user.getId()==null){
            count=userDao.save(user);
        }else{
            count=userDao.update(user);
        }
        if(count==0){
            defaultMsg.setError("操作错误");
        }
        return defaultMsg;
    }

    public DefaultMsg delete(Long id) {
        DefaultMsg defaultMsg = new DefaultMsg();
        Integer count = userDao.delete(id);
        if(count==0){
            defaultMsg.setError("删除失败");
        }
        return defaultMsg;
    }
}
